﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Collections.ObjectModel;

namespace DotNetFeatures
{
    public class Employee
    {
        public string Name;
        public int Id;
        public double Salary;
        public int CompanyId;
        public int DeptId;
        public string City;
    }

    public class Company
    {
        public string CompanyName;
        public int CompanyId;
        public IEnumerable<int> Departments;
    }

    public class Department
    {
        public string DeptName;
        public int DeptId;
        public int CompanyId;
        public IEnumerable<int> Employees;
    }

    public class WorkForce
    {
        public List<Employee> E;
        public List<Company> C;
        public List<Department> D;
    }

    public  class WFConstants
    {
        public static readonly ReadOnlyCollection<string> CityNames = 
            new ReadOnlyCollection<string>( new string[]{ "Hyd", "ncr", "blr", "chennai", "mumbai", "pune", "kolkata", "chd" });
        public static readonly ReadOnlyCollection<string> DepartmentNames = 
            new ReadOnlyCollection<string>(new string[] { "ENG", "IT", "OPS", "FIN", "HR", "LEGAL" });
        public static readonly ReadOnlyCollection<string> CompanyNames = 
            new ReadOnlyCollection<string>(new string[] { "MSFT", "GOOG", "YHOO", "FCBK", "DELL", "ORCL", "IBM" });
    }

    public class WFUtils
    {
        public static WorkForce BuildWorkforce(int size = 10000)
        {

            List<Employee> employees = new List<Employee>();
            List<Company> companies = new List<Company>();
            List<Department> departments = new List<Department>();

            WorkForce wf = new WorkForce();

            wf.E = employees;
            wf.D = departments;
            wf.C = companies;

            string filePath = @"..\..\Resources\names1.txt";
            if (File.Exists(filePath))
            {
                string[] arr = File.ReadAllLines(filePath);
                
                // Randomize the input
                Random rand = new Random();
                for (int i = 0; i < arr.Length; i++)
                {
                    int randNum = rand.Next(0, arr.Length - 1);

                    string s = arr[i];
                    arr[i] = arr[randNum];
                    arr[randNum] = s;
                }
                int eCounter = 0;
                int cCounter = 0;
                int dCounter = 0;
                string city = WFConstants.CityNames[0] + "_" + cCounter;

                foreach (var s in arr)
                {
                    var e = new Employee();
                    e.Name = s;
                    e.Id = eCounter++;
                    e.DeptId = dCounter;
                    e.CompanyId = cCounter;
                    e.City = city;
                    e.Salary = rand.Next(1, 25) * 100000 * Math.Pow(1.1, rand.Next(1, 2));
                    employees.Add(e);


                    if (eCounter % 100 == 0)
                    {
                        var d = new Department();
                        d.DeptId = dCounter;
                        d.DeptName = WFConstants.DepartmentNames[rand.Next(0, WFConstants.DepartmentNames.Count - 1)] + "_" + dCounter;
                        d.CompanyId = cCounter;
                        d.Employees = employees.Select(x => x).Where(x => x.DeptId == dCounter).Select(x => x.Id);
                        departments.Add(d);
                        dCounter++;
                    }

                    if (eCounter % 1000 == 0)
                    {
                        var c = new Company();
                        c.CompanyId = cCounter;
                        c.CompanyName = WFConstants.CompanyNames[rand.Next(0, WFConstants.CompanyNames.Count - 1)] + "_" + cCounter;
                        c.Departments = departments.Select(x => x).Where(x => x.CompanyId == cCounter).Select(x => x.DeptId);
                        companies.Add(c);
                        cCounter++;
                        city = WFConstants.CityNames[rand.Next(0, WFConstants.CityNames.Count - 1)] + "_" + cCounter;
                    }
                }
            }

            return wf;
        }
    }
}
